﻿using DFGK.IService.LY;
using DFGK.Service.LY;
using DFGK_WMS.Common;
using DFGK_WMS.Model.Models.LyModel;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using NPOI.SS.Formula.Functions;
using System.Collections.Generic;
using System.Net.Mime;

namespace DFGK_WMS.WebAPI.Controllers.PC.装备智能采购辅助软件
{
    /// <summary>
    /// 供应商对账明细
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    [ApiExplorerSettings(GroupName = "PC")]
    public class SupplierReconciliationController : BaseController
    {
        private readonly ISupplierReconciliationService _supplierReconciliationService;
        public SupplierReconciliationController(IWebHostEnvironment environment, ISupplierReconciliationService supplierReconciliationService) : base(environment)
        {
            _supplierReconciliationService = supplierReconciliationService;
        }
        /// <summary>
        /// 添加或修改供应商对账明细
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult AddOrModefiySupplierReconciliation(SupplierReconciliation model)
        {
            if (model.Id > 0)
            {
                model.ModifyDate = DateTime.Now;
                model.ModifyUserId = base.BaseGetNowUserId();
                model.ModifyUserName = base.BaseGetNowUserName();
                bool result = _supplierReconciliationService.AddOrModefiySupplierReconciliation(model, out string message);
                if (result)
                {
                    return Ok(BaseResult.Result(true, "修改成功！", "200"));
                }
                return Ok(BaseResult.Result(false, "修改失败！" + message, "500"));
            }
            else
            {
                model.CreateUserName = base.BaseGetNowUserName();
                model.CreateUserId = base.BaseGetNowUserId();
                model.CreateDate = DateTime.Now;
                bool result = _supplierReconciliationService.AddOrModefiySupplierReconciliation(model, out string message);
                if (result)
                {
                    return Ok(BaseResult.Result(true, "添加成功！", "200"));
                }
                return Ok(BaseResult.Result(false, "添加失败！" + message, "500"));


            }
        }
        /// <summary>
        /// 删除供应商对账明细
        /// </summary>
        /// <param name="supplierReconciliationId"></param>
        /// <returns></returns>
        [HttpDelete]
        public IActionResult DelSupplierReconciliation(long supplierReconciliationId)
        {
            if (supplierReconciliationId <= 0)
            {
                return Ok(BaseResult.Result("400", "Id有误！"));
            }
            var result = _supplierReconciliationService.DelSupplierReconciliation(supplierReconciliationId);
            if (result)
            {
                return Ok(BaseResult.Result(true, "删除成功！", "200"));
            }
            return Ok(BaseResult.Result(false, "删除失败！", "500"));
        }
        /// <summary>
        /// 根据id查询
        /// </summary>
        /// <param name="supplierReconciliationId"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetSupplierReconciliation(long supplierReconciliationId)
        {
            if (supplierReconciliationId <= 0)
            {
                return Ok(BaseResult.Result("400", "Id有误！"));
            }
            var shareInfo = _supplierReconciliationService.GetSupplierReconciliation(supplierReconciliationId);
            if (shareInfo != null)
            {
                return Ok(BaseResult.Result(shareInfo, "查询成功！", "200", 0));
            }
            return Ok(BaseResult.Result(null, "查询失败！", "500", 0));
        }
        /// <summary>
        /// 分页查询
        /// </summary>
        /// <param name="name"></param>
        /// <param name="startDate"></param>
        /// <param name="endDate"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetSupplierReconciliationData(string? name, DateTime? startDate, DateTime? endDate, int pageIndex = 1, int pageSize = 15)
        {

            if (pageIndex <= 0 || pageSize <= 0)
            {
                BaseResult.Result("400", "分页条件有误！");
            }
            var list = _supplierReconciliationService.GetSupplierReconciliationData(name, startDate, endDate, out var totalCount, pageIndex, pageSize);
            if (list != null)
            {
                return Ok(BaseResult.Result(list, "查询成功！", "200", totalCount));
            }
            return Ok(BaseResult.Result(null, "查询失败！", "500", 0));
        }
        /// <summary>
        /// 按供应商名称统计对账金额
        /// </summary>
        /// <param name="supplierName"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetTotalAmountBySupplier(string supplierName)
        {
            var query = _supplierReconciliationService.GetTotalAmountBySupplier(supplierName);
            if (query != 0)
            {
                return Ok(BaseResult.Result(query, "查询成功！", "200"));
            }
            return Ok(BaseResult.Result(null, "查询失败！", "500", 0));
        }


        [HttpGet]
        public IActionResult SupplierExport(string? name, DateTime? startDate, DateTime? endDate, int pageIndex = 1, int pageSize = 15)
        {
            if (pageIndex <= 0 || pageSize <= 0)
            {
                pageIndex = 1;
                pageSize = 50;
            }
            var list = _supplierReconciliationService.GetSupplierReconciliationData(name, startDate, endDate, out var totalCount, pageIndex, pageSize);
            var keyValue = new Dictionary<string, string>()
            {
                {nameof(SupplierReconciliation.SupplierName), "供应商名称"},
                {nameof(SupplierReconciliation.ReconciliationDate),"对账时间"},
                {nameof(SupplierReconciliation.Amount),"对账金额"},
                {nameof(SupplierReconciliation.SupplierContact),"供应商联系人"},
                {nameof(SupplierReconciliation.SupplierPhone),"供应商电话"},
                {nameof(SupplierReconciliation.SupplierAddress),"供应商地址"},
                {nameof(SupplierReconciliation.ReconciliationStatus),"对账状态"}
            };
            var fileBytes = Tool.ExportToExcel(list, keyValue);
            return File(fileBytes, MediaTypeNames.Application.Octet, $"供应商对账{DateTime.Now.ToString("yyyyMMddHHmmss")}.xlsx");
        }
    }
}
